package thelist;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import task.TaskState;
import usr.Usr;

import database.Database;


public class thelistTask {
	
	public static int getEnabledTasksCount(Usr usr)
	{
		int enable = 0;
		try {
			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			
			ResultSet rs = stmt.executeQuery(String.format(
					"select count(*) " +
					"from task " +
					"where usr = %d and state = %d",
					usr.getId(), TaskState.RUNNING));
			rs.next();
			
			enable = rs.getInt(1);
			
			stmt.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return enable;
	}
	
	public static int getTotalTasksCount(Usr usr)
	{
		int total = 0;
		try {
			Connection conn = Database.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(String.format(
					"select count(*) " +
					"from task " +
					"where usr = %d and state != %d",
					usr.getId(), TaskState.DELETED));
			rs.next();
			total = rs.getInt(1);
			
			stmt.close();
			conn.close();
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return total;
	}
}
